package com.kr.weitao.util.rest.dataengine;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kr.weitao.util.jdbc.JdbcDataEngine;
import com.kr.weitao.util.rest.condition.Condition;
import com.kr.weitao.util.rest.condition.MysqlCondition;
import com.kr.weitao.util.rest.schema.Column;
import com.kr.weitao.util.rest.schema.Function;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/** Created by paco on 2017/5/27. */
@Log4j2
@Component
public class MySQLDataEngine implements DataDisposeEngine {
  @Autowired private JdbcDataEngine jdbcDataEngine;

  @Override
  public long insert(String sql, Object[] param) {

    return jdbcDataEngine.getJdbcClient().insert(sql, param);
  }

  @Override
  public int update(String sql, Object[] param) {
    return jdbcDataEngine.getJdbcClient().update(sql, param);
  }

  @Override
  public int delete(String sql, Object[] param) {

    return jdbcDataEngine.getJdbcClient().delete(sql, param);
  }

  /**
   * @param function 数据来源
   * @param field 查询字段
   * @param le 过滤条件
   * @param param 排序、分页信息
   * @return
   */
  @Override
  public JSONObject getJSONArray(
          Function function, List<Column> field, LogicExpression le, JSONObject param) {
    JSONObject resultjo = new JSONObject();
    if (param == null) {
      param = new JSONObject();
    }
    int pageNum = param.getIntValue("page_num");
    if (pageNum < 0) {
      pageNum = 0;
    }
    int pageSize = param.getIntValue("page_size");
    if (pageSize <= 0) {
      pageSize = 100;
    } else if (pageSize > 500) {
      pageSize = 100;
    }

    Condition cu = MysqlCondition.getInstance(function);

    cu.setCondition(le);
    String countSql = cu.getCountSql();

    int dataCount = jdbcDataEngine.getJdbcClient().getCount(countSql, cu.getQueryDataParam());

    //region 分页计算
    int beginindex = pageNum * pageSize;
    int endindex = pageSize;
    int yc = dataCount % pageSize; //如果记录总数不为整数页时，值为最后一页的记录数，如果为整数页，则值为0
    int zc = dataCount / pageSize; //记录总数能被分成多少个整数页

    //如果查询记录大于数据记录时，直接返回空数据

    if (dataCount <= beginindex) {
      if (yc > 0) {
        zc = zc + 1;
      }
      resultjo.put("total_count", dataCount);
      resultjo.put("page_count", zc);
      resultjo.put("list", new JSONArray());

      return resultjo;
    }

    if (dataCount < (pageNum + 1) * pageSize) {
      if (yc > 0) {
        endindex = yc;
      }
      beginindex = zc * pageSize;
    }
    if (yc > 0) {
      zc = zc + 1;
    }
    //endregion

    //设置排序
    cu.setOrderBy(param.getJSONArray("order_by"));
    log.debug("----cu:"+param.getJSONArray("order_by"));

    //设置分页
    cu.setPage(beginindex, endindex);

    //查询数据
    JSONArray dataja =
        jdbcDataEngine.getJdbcClient().getJSONArray(cu.getQuerySql(field), cu.getQueryDataParam());
    resultjo.put("data_count", dataCount);
    resultjo.put("page_count", zc);
    resultjo.put("list", dataja);

    return resultjo;
  }

  /**
   * @param function 数据来源
   * @param field 查询字段
   * @param le 过滤条件
   * @param param 排序、分页信息，分页从0开始（默认值0），页大小不能超过500（默认值100）
   * @return
   */
  @Override
  public JSONObject getJSONObject(
      Function function, List<Column> field, LogicExpression le, JSONObject param) {
    JSONObject resultjo = new JSONObject(); //返回数据

    if (param == null) {
      param = new JSONObject();
    }

    Condition cu = MysqlCondition.getInstance(function);

    cu.setCondition(le);

    //设置排序
    cu.setOrderBy(param.getJSONArray("order_by"));

    //设置分页
    cu.setPage(0, 1);

    //查询数据
    JSONObject datajo =
        jdbcDataEngine.getJdbcClient().getJSONObject(cu.getQuerySql(field), cu.getQueryDataParam());
    return datajo;
//    resultjo.put("object", datajo);

//    return resultjo;
  }
}
